home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_400 / 412_01 / include / list.h < prev    next >
Encoding:
C/C++ Source or Header  |  1993-12-27  |  4.3 KB  |  151 lines

  1. #ifndef _list_H_
  2. #define _list_H_
  3.  
  4. #include <stdio.h>
  5. #include "object.h"
  6.  
  7.  
  8. /*                    LIST_NODE_
  9.  
  10.     Class LIST_NODE_ defines objects that make up a linked list, it should
  11.     be used in conjuntion with class LIST_. Class LIST_NODE_ stores data
  12.     in the form of pointers to VOBJECT_'s. A special feature of this class
  13.     is that the data may or may not be deleted when the LIST_NODE_ pointing
  14.     to this data gets destroyed. This behaviour depends on the value of
  15.     the static variable NODE_::destroy, which is set by class LIST_.
  16.  
  17. */
  18.      
  19. #define DEALLOC 0
  20. #define NODEALLOC 1
  21.  
  22. class LIST_NODE_
  23. {
  24.     public:
  25.         LIST_NODE_();
  26.         LIST_NODE_(VOBJECT_ &);
  27.         LIST_NODE_(VOBJECT_ &, LIST_NODE_ *, LIST_NODE_ *);
  28.         ~LIST_NODE_();
  29.         void setdata(VOBJECT_ &);
  30.         VOBJECT_ *getdata() const;
  31.         void setnext(LIST_NODE_ *);
  32.         LIST_NODE_ *getnext() const;
  33.         void setprev(LIST_NODE_ *);
  34.         LIST_NODE_ *getprev() const;
  35.         static int destroy;            // should data be deleted when
  36.     private:                // listnode is destroyed?
  37.         VOBJECT_ *data;            // pointer to data field
  38.         LIST_NODE_ *next,
  39.                    *prev;
  40. };
  41.  
  42.  
  43. /*
  44.             LIST_
  45.  
  46.     Class LIST_ creates an un-ordered list of (pointers to) VOBJECT_'s.
  47.     This means that objects that are to be stored in a LIST_ object must 
  48.     derived from class VOBJECT_.
  49.  
  50.     When objects are removed from a list (by calling, e.g, remove_head())
  51.     they may be kept or destroyed, i.e., get deallocated. This behaviour 
  52.     depends on the value passed to the remove_ and clear() functions, the
  53.     default value is NODEALLOC: don't destroy the object.
  54.  
  55.     Similarly, objects may be kept or destroyed when the destructor of the
  56.     list is called, this behaviour depends on the value that is initially
  57.     passed to LIST_'s constructor, or on the value passed to setdestruct(int):
  58.     DEALLOC_ON_DESTRUCT or NO_DEALLOC_ON_DESTRUCT, by default the objects
  59.     in the list will get deallocated when LIST_'s destructor is called.
  60.  
  61. */ 
  62.  
  63. #define DEALLOC_ON_DESTRUCT 0
  64. #define NO_DEALLOC_ON_DESTRUCT 1
  65.  
  66. class LIST_
  67. {
  68.     friend class LIST_ITERATOR_;
  69.  
  70.     public:
  71.         LIST_(int dstr = DEALLOC_ON_DESTRUCT);
  72.               // deallocate objects on destruct, default 
  73.         ~LIST_();
  74.  
  75.         void addtohead(VOBJECT_ &);
  76.         void addtotail(VOBJECT_ &);
  77.  
  78.         VOBJECT_ *gethead() const;
  79.     VOBJECT_ *gettail() const;
  80.     int getcount() const;
  81.         VOBJECT_ *lookup(VOBJECT_ &);
  82.  
  83.         void remove_head(int destroy = NODEALLOC);
  84.                // destroy : DEALLOC = object gets deallocated
  85.                // NODEALLOC = don't deallocate object, default
  86.         void remove_tail(int destroy = NODEALLOC);
  87.         void remove_found(int destroy = NODEALLOC);
  88.     void clear(int destroy = NODEALLOC);
  89.  
  90.     void setdestruct(int);          // get value of deallocondestr
  91.     int getdestruct() const;    // set value of deallocondestr
  92.  
  93.     protected:         // protected because SORTEDLIST_ needs access
  94.     void remove_node(LIST_NODE_ *, int dstr);
  95.  
  96.     LIST_NODE_ *head,
  97.                    *tail,
  98.                    *found;
  99.         int nodecount,
  100.         deallocondestr;
  101. };
  102.  
  103.  
  104.  
  105. /*
  106.                   SORTEDLIST_
  107.  
  108.    Class SORTEDLIST_ creates an ordered list of (pointers to) SVOBJECT_ 's.
  109.    Therefore, objects to be stored in a SORTEDLIST_ object muct be derived
  110.    from class SVOBJECT_. Class SORTEDLIST_ is derived from class LIST_.
  111.  
  112. */
  113.  
  114. class SORTEDLIST_ : public LIST_
  115. {
  116.     public:
  117.         SORTEDLIST_(int dstr = DEALLOC_ON_DESTRUCT);
  118.         void insert(SVOBJECT_ &);
  119. };
  120.  
  121.  
  122.  
  123. /*
  124.           LIST_ITERATOR_
  125.  
  126.     Class LIST_ITERATOR_ iterates through the objects stored in a list.
  127.     While walking through the list objects may be removed, by calling
  128.     remove(), see also list.h! When an object is removed the current
  129.     pointer of the list iterator moves on one node (or back one node if it
  130.     is located on the last node).
  131.  
  132. */
  133.  
  134. class LIST_ITERATOR_
  135. {
  136.     public:
  137.     LIST_ITERATOR_(LIST_ &);
  138.     VOBJECT_ *getfirst();
  139.     VOBJECT_ *getlast();
  140.     VOBJECT_ *getnext();
  141.     VOBJECT_ *getprev();
  142.     VOBJECT_ *getitem();
  143.     void remove(int destroy = NODEALLOC);
  144.         // remove current object and move on one node
  145.     private:
  146.     LIST_ *mine;
  147.     LIST_NODE_ *current;
  148. };
  149.  
  150. #endif
  151.